      SUBROUTINE SECBD1(SY1,SY2,SY3,CIY)
C
      IMPLICIT REAL*8(A-H,O-Z)
C
      COMMON/CSECBD/SECM,SECI(3,3),ZBAR2(3),YI02(3),ZZ2(3,3),ZZ2Y(3,3)
     1             ,YZ2(3),YZT(3),ZB(3),YIZ02(3)
C
      COMMON/DEBUG2/IOUT,JOUT,KLUGE
C
      COMMON/ISECBD/I2BDY,NDOF2,IRAST,NDEP2,IARST(3),IRSCY(3)
C
      COMMON/RPOOL1/DUMV1(80),YBCM(3),DUMV2(42)
C
      COMMON/SBDOUT/ANG2(3),ANG2D(3),ANG2DD(3),B(3,3),B0B(3,3)
     1             ,OM2(3),SALP,CALP,SBET,CBET,H2B(3),Y2D(3)
C
      COMMON/SECBDW/GAMMA2(3,6),DELTA2(3,3),DELT2(3,3),DELT2I(3,3)
     1             ,COMQ(3,3),QGAM(3,6),DIQGM(3,6),DALP(6,6)
     2             ,VP(3),VPS(6)
C
      COMMON/SECICS/GAM20,ALP20,BET20,B0(3,3)
C
      COMMON/VARBLS/DEP(150),DER(150)
C
C
      EQUIVALENCE (ANG2(1),GAM2),(ANG2(2),ALP2),(ANG2(3),BET2)
      EQUIVALENCE (ANG2D(1),GAM2D),(ANG2D(2),ALP2D),(ANG2D(3),BET2D)
C
C
      DIMENSION CIY(3,3)
      DIMENSION  DUM1(3,3),YYZ2(3,3)
      DIMENSION DUM2(3,3),DUM3(3,3),II(3),JJ(3)
      DIMENSION C(6),MT(6)
      DIMENSION PV(3)
      DIMENSION V1(3),V2(3)
C
      DATA II/2,3,1/,JJ/3,1,2/
      DATA MAXD/3/
C
C
C     SECBD1 IS CALLED FROM FNDALP TO INCLUDE SECONDARY BODY
C     C-G OFFSET AND INERTIA PROPERTIES IN SYSTEM PROPERTIES
C
      IF(I2BDY.EQ.0) RETURN
C
      DO 5 I=1,3
      ANG2(I)=0.0D0
      ANG2D(I)=0.0D0
      ANG2DD(I)=0.0D0
      ZB(I)=SECM*ZBAR2(I)
    5 CONTINUE
C
      IF(IRAST.EQ.0) GO TO 10
C
      CALL RAST2(ANG2,ANG2D,ANG2DD)
C
      GO TO 15
C
   10 CONTINUE
C
C     EXTRACT SECONDARY STATE VARIABLES FROM DEP
C
      IF(NDOF2.EQ.0) GO TO 15
C
      I1=NDEP2
      I2=I1+NDOF2
      BET2=DEP(I1)
      BET2D=DEP(I2)
C
      IF(NDOF2.EQ.1) GO TO 15
C
      I1=I1+1
      I2=I2+1
      GAM2=DEP(I1)
      GAM2D=DEP(I2)
C
      IF(NDOF2.EQ.2) GO TO 15
C
      I1=I1+1
      I2=I2+1
      ALP2=DEP(I1)
      ALP2D=DEP(I2)
C
   15 CONTINUE
C
      CALL DTR312(GAM2,ALP2,BET2,B)
C
      CALL MPYMAT(B0,B,DUM1,1,1,B0B,DUM1)
C
C     CONSTRUCT ANGULAR RATES
C
      SBET=DSIN(BET2)
      CBET=DCOS(BET2)
      SALP=DSIN(ALP2)
      CALP=DCOS(ALP2)
      WS=GAM2D*CALP
      OM2(1)=-WS*SBET+ALP2D*CBET
      OM2(2)=BET2D+GAM2D*SALP
      OM2(3)=WS*CBET+ALP2D*SBET
C
      V1(1)=OM2(2)*ZBAR2(3)-OM2(3)*ZBAR2(2)
      V1(2)=OM2(3)*ZBAR2(1)-OM2(1)*ZBAR2(3)
      V1(3)=OM2(1)*ZBAR2(2)-OM2(2)*ZBAR2(1)
C
      CALL MATV(1,B0B,V1,V2)
C
      IF(IOUT.EQ.1) GO TO 16
      WRITE(6,5999)
 5999 FORMAT('0',10X,'DEBUGGING OUTPUT FROM SECBD1'/)
      WRITE(6,6000) GAM2,ALP2,BET2,GAM2D,ALP2D,BET2D,OM2
 6000 FORMAT(' ',1P10E13.5)
   16 CONTINUE
C
C     TRANSFORM INERTIA PROPERTIES TO BODY FRAME
C
      CALL MPYMAT(B0B,ZZ2,B0B,2,2,DUM1,ZZ2Y)
C
C     CALCULATE C-G OFFSET OF SECONDARY BODY
C
      CALL MATV(1,B0B,ZBAR2,YZ2)
C
      CALL ADDV(YI02,YZ2,YZT)
C
      SY1=SECM*YZT(1)
      SY2=SECM*YZT(2)
      SY3=SECM*YZT(3)
C
      DO 20 I=1,3
      YBCM(I)=YBCM(I)+SECM*YZT(I)
      Y2D(I)=SECM*V2(I)
      DO 20 J=1,3
      YYZ2(I,J)=SECM*(YI02(I)*YI02(J)+YI02(I)*YZ2(J)+YI02(J)*YZ2(I))
     1          +ZZ2Y(I,J)
      CIY(I,J)=CIY(I,J)+YYZ2(I,J)
   20 CONTINUE
C
      IF(IOUT.EQ.1) GO TO 21
      WRITE(6,6000) SY1,SY2,SY3,YBCM
      WRITE(6,6000) CIY
   21 CONTINUE
C
C
C     COUPLING WITH SYSTEM TRANSLATION EQUATIONS
C
      DO 25 I=1,3
      I1=II(I)
      J1=JJ(I)
      DO 25 J=1,3
      GAMMA2(I,J)=ZB(I1)*B0B(J,J1)-ZB(J1)*B0B(J,I1)
   25 CONTINUE
C
C     COUPLING WITH SYSTEM ROTATION EQUATION
C
      CALL MATV(2,B0B,YI02,YIZ02)
C
      TRAO2=0.0D0
C
      DO 30 I=1,3
      TRAO2=TRAO2+YIZ02(I)*ZB(I)+ZZ2(I,I)
      DO 30 J=1,3
      DUM2(I,J)=-YIZ02(I)*ZB(J)-ZZ2(I,J)
   30 CONTINUE
C
      DO 35 I=1,3
      DUM2(I,I)=DUM2(I,I)+TRAO2
   35 CONTINUE
C
      DO 37 I=1,3
      DO 37 J=1,3
      WS=0.0D0
      DO 36 K=1,3
      WS=WS+DUM2(I,K)*B0B(J,K)
   36 CONTINUE
      DUM3(I,J)=WS
   37 CONTINUE
C
C     LOAD INTO GAMMA2
C
      DO 40 I=1,3
      H2B(I)=0.0D0
      DO 40 J=1,3
      J1=J+3
      GAMMA2(I,J1)=DUM3(I,J)
      H2B(I)=H2B(I)+DUM3(J,I)*OM2(J)
   40 CONTINUE
C
      IF(NDOF2.EQ.0) GO TO 100
C
C
      DO 42 I=1,3
      DO 42 J=1,3
      COMQ(I,J)=0.0D0
   42 CONTINUE
C
      COMQ(1,2)=1.0D0
      COMQ(2,1)=-CALP*SBET
      COMQ(2,2)=SALP
      COMQ(2,3)=CALP*CBET
      COMQ(3,1)=CBET
      COMQ(3,3)=SBET
C
      DO 45 I=1,3
      DO 45 J=1,6
      WS=0.0D0
      DO 44 K=1,3
      WS=WS+COMQ(I,K)*GAMMA2(K,J)
   44 CONTINUE
      QGAM(I,J)=WS
      DIQGM(I,J)=0.0D0
   45 CONTINUE
C
      DO 47 I=1,3
      DO 47 J=1,3
      WS=0.0D0
      DO 46 K=1,3
      WS=WS+COMQ(I,K)*DELTA2(K,J)
   46 CONTINUE
      DUM1(I,J)=WS
   47 CONTINUE
C
      DO 50 I=1,3
      DO 50 J=1,3
      WS=0.0D0
      DO 49 K=1,3
      WS=WS+DUM1(I,K)*COMQ(J,K)
   49 CONTINUE
      DELT2(I,J)=WS
      DELT2I(I,J)=WS
   50 CONTINUE
C
      CALL INVERT(DELT2I,NDOF2,MAXD,ID,C,MT)
C
      DO 55 I=1,NDOF2
      DO 55 J=1,6
      WS=0.0D0
      DO 54 K=1,NDOF2
      WS=WS+DELT2I(I,K)*QGAM(K,J)
   54 CONTINUE
      DIQGM(I,J)=WS
   55 CONTINUE
C
      WS1=GAM2D*ALP2D
      WS2=GAM2D*BET2D*CALP
      WS3=ALP2D*BET2D
C
      PV(2)=WS1*CALP
      WS1=WS1*SALP
      PV(1)=WS1*SBET-WS2*CBET-WS3*SBET
      PV(3)=-WS1*CBET-WS2*SBET+WS3*CBET
C
      DO 60 I=1,3
      I3=I+3
      WS1=0.0D0
      WS2=0.0D0
      WS3=0.0D0
      DO 59 J=1,3
      WS1=WS1+DUM1(I,J)*PV(J)
      WS2=WS2+GAMMA2(J,I)*PV(J)
      WS3=WS3+GAMMA2(J,I3)*PV(J)
   59 CONTINUE
      VP(I)=WS1
      VPS(I)=WS2
      VPS(I3)=WS3
   60 CONTINUE
C
      DO 65 I=1,6
      DO 65 J=1,6
      WS=0.0D0
      DO 64 K=1,NDOF2
      WS=WS+QGAM(K,I)*DIQGM(K,J)
   64 CONTINUE
      DALP(I,J)=WS
   65 CONTINUE
C
      IF(IOUT.EQ.1) GO TO 70
      WRITE(6,6001) ((GAMMA2(I,J),J=1,6),I=1,3)
 6001 FORMAT(' ',1P6E14.6)
      WRITE(6,6000) DELTA2
      WRITE(6,6000) DELT2
      WRITE(6,6000) DELT2I
      WRITE(6,6000) COMQ
      WRITE(6,6001) (( QGAM(I,J),J=1,6),I=1,3)
      WRITE(6,6001) ((DIQGM(I,J),J=1,6),I=1,3)
      WRITE(6,6001) (( DALP(I,J),J=1,6),I=1,6)
   70 CONTINUE
C
C
  100 CONTINUE
C
C
      RETURN
C
      END
